using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Configuration;



namespace THSLib
{
    public class THSTableAdapter:Component
    {
        private String _tableName;
        private String _commandText;
        private String _updateCommand;
        private SqlParameter[] _updateParams;

        private String _deleteCommand;
        private SqlParameter[] _deleteParams;

        private bool _isStoredProcedure;
        private int _dataSetID;        
        private SqlDataAdapter _adapter;
        private SqlConnection _connecttion;
        private bool _clearBeforeFill;

        public THSTableAdapter(String strTableName,int iTHSDataSetID)
        {
            TableName = strTableName;
            DataSetID = iTHSDataSetID;
            ClearBeforeFill = true;
        }

        public THSTableAdapter(String strTableName, String strSelectCommand,bool bIsStoredProcedure)
        {
            TableName = strTableName;
            CommandText = strSelectCommand;
            IsStoredProcedure = bIsStoredProcedure;
            ClearBeforeFill = true;
        }        

        public THSTableAdapter(String strTableName,String strSelectCommand,String strUpdateCommand,String strDeleteCommand,bool bIsStoredProcedure)
        {
            TableName=strTableName;
            CommandText=strSelectCommand;
            UpdateCommand = strUpdateCommand;
            DeleteCommand = strDeleteCommand;
            IsStoredProcedure = bIsStoredProcedure;
            ClearBeforeFill = true;
        }

        public THSTableAdapter(String strTableName, String strSelectCommand, String strUpdateCommand, SqlParameter[] updateParams, String strDeleteCommand, SqlParameter[] deleteParams, bool bIsStoredProcedure)
        {
            TableName = strTableName;
            CommandText = strSelectCommand;
            UpdateCommand = strUpdateCommand;
            _updateParams = updateParams;
            DeleteCommand = strDeleteCommand;
            _deleteParams = deleteParams;

        }
        private void InitConnection()
        {
            this._connecttion = new SqlConnection();
            this._connecttion.ConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
        }

        private void InitAdapter()
        {
            
            this._adapter = new SqlDataAdapter();
            DataTableMapping tableMapping = new DataTableMapping();
            tableMapping.SourceTable = TableName;
            tableMapping.DataSetTable = TableName;
            DataSet dsTableColumns = THSDbUtil.GetTableColumns(TableName);
            if (dsTableColumns.Tables.Count > 0)
            {
                foreach (DataRow rowColumn in dsTableColumns.Tables[0].Rows)
                {
                    tableMapping.ColumnMappings.Add(rowColumn["COLUMN_NAME"].ToString(), rowColumn["COLUMN_NAME"].ToString());
                }
            }
            this._adapter.TableMappings.Add(tableMapping);
            if (DataSetID > 0)
            {
                //Init Select Command
                this._adapter.SelectCommand = new SqlCommand();
                this._adapter.SelectCommand.Connection = this.Connection;
                this._adapter.SelectCommand.CommandText = GetSelectCommandText();
                this._adapter.SelectCommand.CommandType = CommandType.Text;
               
            }
            else
            {
                //Init Select Command
                this._adapter.SelectCommand = new SqlCommand();
                this._adapter.SelectCommand.Connection = this.Connection;
                this._adapter.SelectCommand.CommandText = CommandText;
                if (IsStoredProcedure)
                    this._adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
                else
                    this._adapter.SelectCommand.CommandType = CommandType.Text;

                //Init Update Command
                this._adapter.UpdateCommand = new SqlCommand();
                this._adapter.UpdateCommand.Connection = this.Connection;
                this._adapter.UpdateCommand.CommandText = UpdateCommand;
                if (IsStoredProcedure)
                    this._adapter.UpdateCommand.CommandType = CommandType.StoredProcedure;
                else
                    this._adapter.UpdateCommand.CommandType = CommandType.Text;
                //Add Parameter
                if (_updateParams != null)
                {
                    if (_updateParams.Length > 0)
                    {
                        foreach (SqlParameter param in _updateParams)
                        {
                            _adapter.UpdateCommand.Parameters.Add(param);
                        }
                    }
                }

                //Init Delete Command
                this._adapter.DeleteCommand = new SqlCommand();
                this._adapter.DeleteCommand.Connection = this.Connection;
                this._adapter.DeleteCommand.CommandText = DeleteCommand;
                if (IsStoredProcedure)
                    this._adapter.DeleteCommand.CommandType = CommandType.StoredProcedure;
                else
                    this._adapter.DeleteCommand.CommandType = CommandType.Text;
                //Add Parameter
                if (_deleteParams != null)
                {
                    if (_deleteParams.Length > 0)
                    {
                        foreach (SqlParameter param in _deleteParams)
                        {
                            _adapter.DeleteCommand.Parameters.Add(param);
                        }
                    }
                }
            }
        }

        private String GetSelectCommandText()
        {
            String strSelectCommandText = String.Empty;
            STTHSDataTablesController objSTTHSDataTablesController = new STTHSDataTablesController();
            STTHSDataTablesInfo objSTTHSDataTablesInfo = objSTTHSDataTablesController.GetTHSDataTableByTHSDataSetIDAndTHSDataTableName(DataSetID, TableName);
            if (objSTTHSDataTablesInfo != null)
                strSelectCommandText = objSTTHSDataTablesInfo.STTHSDataTableSelectCommand;
            return strSelectCommandText;
        }


        public SqlDataAdapter Adapter
        {
            get
            {
                if ((this._adapter == null))
                {
                    this.InitAdapter();
                }
                return this._adapter;
            }
        }        

        public bool ClearBeforeFill
        {
            get
            {
                return this._clearBeforeFill;
            }
            set
            {
                this._clearBeforeFill = value;
            }
        }

        public String TableName
        {
            get
            {
                return this._tableName;
            }
            set
            {
                this._tableName = value;
            }
        }

        public String CommandText
        {
            get
            {
                return this._commandText;
            }
            set
            {
                this._commandText = value;
            }
        }

        public String UpdateCommand
        {
            get
            {
                return this._updateCommand;
            }
            set
            {
                _updateCommand = value;
            }
        }

        public String DeleteCommand
        {
            get
            {
                return this._deleteCommand;
            }
            set
            {
                _deleteCommand = value;
            }
        }

        public bool IsStoredProcedure
        {
            get
            {
                return this._isStoredProcedure;
            }
            set
            {
                this._isStoredProcedure = value;
            }
        }

        public int DataSetID
        {
            get
            {
                return this._dataSetID;
            }
            set
            {
                this._dataSetID = value;
            }
        }

        internal SqlConnection Connection
        {
            get
            {
                if (this._connecttion == null)
                {
                    this.InitConnection();
                }
                return this._connecttion;
            }
            set
            {
                this._connecttion = value;
                if (this.Adapter.SelectCommand != null)
                    this.Adapter.SelectCommand.Connection = value;
            }
        }

        [System.ComponentModel.Design.HelpKeywordAttribute("vs.data.TableAdapter")]
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Fill, true)]
        public virtual int Fill(DataTable table)
        {            
            if ((this.ClearBeforeFill == true))
            {
                table.Clear();
            }
            int returnValue = this.Adapter.Fill(table);
            return returnValue;                       
        }

        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [System.ComponentModel.Design.HelpKeywordAttribute("vs.data.TableAdapter")]
        public virtual int Update(System.Data.DataRow dataRow)
        {
            return this.Adapter.Update(new System.Data.DataRow[] {
                        dataRow});
        }
    }
}
